
#include "showaqi.h"

ShowAqi::ShowAqi( QWidget *parent)
    : QWidget( parent)
{


    this->setMinimumSize(800,480);
    this->setMaximumSize(800,480);


    aqiInfo = new ShowLabel(this);
    aqiInfo->addFontSize(8);
    aqiInfo->setAlignment(Qt::AlignHCenter);
    aqiInfo->setGeometry(QRect(150,60,650,40));
    aqiInfo->setText(tr("细颗粒物指数 (PM 2.5)"));


    pm25_net = 500;

    int x;
    x = get_x_point(pm25_net);
    //net value
    aqiIndexNet = new QLabel(this);
    aqiIndexNet->setAlignment(Qt::AlignHCenter);
    aqiIndexNet->setGeometry(QRect(x, 100, 60, 60));
    aqiIndexNet->setPixmap(QPixmap(":/images/index_net.png"));
    aqiIndexNet->setScaledContents(true);

    aqiValNet = new ShowLabel(this);
    aqiValNet->addFontSize(0);
    aqiValNet->setAlignment(Qt::AlignHCenter);
    aqiValNet->setGeometry(QRect(x, 120, 60, 60));
    aqiValNet->setText(QString::number(pm25_net));

    net_falg = new ShowLabel(this);
    net_falg->setAlignment(Qt::AlignHCenter);
    net_falg->setGeometry(QRect(x, 110, 60, 60));
    net_falg->setText(tr("网络"));

    pm25_local = 300;
    x = get_x_point(pm25_local);
    //local value
    aqiIndexLocal = new QLabel(this);
    aqiIndexLocal->setAlignment(Qt::AlignHCenter);
    aqiIndexLocal->setGeometry(QRect(x, 100, 60, 60));
    aqiIndexLocal->setPixmap(QPixmap(":/images/index_local.png"));
    aqiIndexLocal->setScaledContents(true);

    aqiValLocal = new ShowLabel(this);
    aqiValLocal->addFontSize(0);
    aqiValLocal->setAlignment(Qt::AlignHCenter);
    aqiValLocal->setGeometry(QRect(x, 180, 60, 60));
    aqiValLocal->setText(QString::number(pm25_local));

    local_falg = new ShowLabel(this);
    local_falg->setAlignment(Qt::AlignHCenter);
    local_falg->setGeometry(QRect(x, 225, 60, 60));
    local_falg->setText(tr("本地"));


    aqiPic = new QLabel(this);
    aqiPic->setAlignment(Qt::AlignHCenter);
    aqiPic->setGeometry(QRect(220, 190, 520, 80));
    aqiPic->setPixmap(QPixmap(":/images/aqi.png"));
    aqiPic->setScaledContents(true);

    /*
    aqiItem = new ShowLabel(this);
    aqiItem->addFontSize(6);
    aqiItem->setAlignment(Qt::AlignHCenter);
    aqiItem->setGeometry(QRect(210, 380, 800, 60));
    aqiItem->setText(tr("上读数为网络，下读数为本地．"));
*/
    aqiValue = new ShowLabel(this);
    aqiValue->addFontSize(1);
    aqiValue->setAlignment(Qt::AlignHCenter);
    aqiValue->setGeometry(QRect(150, 360, 650, 40));
    aqiValue->setText(tr("武汉     AQI(空气质量指数)  94     PM2.5  45     PM10  56"));

}


int ShowAqi::get_x_point(int val)
{
    int base = 190;

    if(val>0){
        if(val>500) return base+480;
        if(val<0) return base;

        if(val>=0 && val<=200)
            return val*1.6 + base ;

        if(val>200 && val<=300)
            return (val-200)*0.8 + base + 320;

        if(val>300 && val<=500)
            return (val-300)/2*0.8 + base + 400;
    }else{
        return base;
    }

}

void ShowAqi::updateVal(QString *aqiArr)
{
    pm25_net = aqiArr[0].toInt();
    pm25_local = aqiArr[3].toInt();

    int x;
    x = get_x_point(pm25_net);
    //net value
    aqiIndexNet->setGeometry(QRect(x, 130, 60, 60));

    aqiValNet->setGeometry(QRect(x, 145, 60, 60));
    aqiValNet->setText(QString::number(pm25_net));

    net_falg->setGeometry(QRect(x, 110, 60, 60));
//    qDebug() << "pm25_net=" << x;

    x = get_x_point(pm25_local);
    //local value
    aqiIndexLocal->setGeometry(QRect(x, 240, 60, 60));

    aqiValLocal->setGeometry(QRect(x, 265, 60, 60));
    aqiValLocal->setText(QString::number(pm25_local));

    local_falg->setGeometry(QRect(x, 300, 60, 60));

    aqiValue->setText(aqiArr[4] + tr("    AQI(空气质量指数)  ") + aqiArr[2] + tr("    PM2.5  ") + aqiArr[0] + tr("     PM10  ") + aqiArr[1]);

 //   qDebug() << "pm25_local=" << x;
}
